package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.ControlDeviceRequest;
import smartlifev30.com.sdk.bean.request.GetDeviceStatusRequest;
import smartlifev30.com.sdk.bean.request.IrLearnRequest;
import smartlifev30.com.sdk.bean.response.DeviceStatusResponse;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;

/**
 * Created by tys on 2018/8/15.
 */

public class ControlMgmtImpl implements IControlMgmt{

    @Override
    public void getDeviceStatus(@NonNull String userName, @NonNull String token, @NonNull String sn,
                                @Nullable String deviceType, @Nullable List<Integer> deviceId,
                                @NonNull CallBack<DeviceStatusResponse> callback) {
        GetDeviceStatusRequest getDeviceStatusRequest = new GetDeviceStatusRequest();
        getDeviceStatusRequest.setFrom(userName);
        getDeviceStatusRequest.setTo(sn);
        getDeviceStatusRequest.setMsg_id(callback.getMsgId());
        getDeviceStatusRequest.setMsg_class(ConstantGateway.MSG_CLASS_CONTROL);
        getDeviceStatusRequest.setMsg_name("device_state_get");
        getDeviceStatusRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        getDeviceStatusRequest.setToken(token);
        List<GetDeviceStatusRequest.DeviceList> list = null;
        if (deviceId != null) {
            list = new ArrayList<>();
            for (Integer id : deviceId) {
                list.add(getDeviceStatusRequest.new DeviceList(id));
            }
        }
        //二选其一  (现在两个都有)
        getDeviceStatusRequest.setDevice_list(list);
        getDeviceStatusRequest.setDevice(getDeviceStatusRequest.new Device(deviceType));
        NetManager.getInstance().sendMessage(JsonUtil.toString(getDeviceStatusRequest));
    }

    @Override
    public void controlDevice(@NonNull String userName, @NonNull String token, @NonNull String sn,
                              @NonNull ControlDeviceRequest.Device device, @NonNull CallBack<LoginResponse> callBack) {
        ControlDeviceRequest controlDeviceRequest = new ControlDeviceRequest();
        controlDeviceRequest.setFrom(userName);
        controlDeviceRequest.setTo(sn);
        controlDeviceRequest.setMsg_id(callBack.getMsgId());
        controlDeviceRequest.setMsg_class(ConstantGateway.MSG_CLASS_CONTROL);
        controlDeviceRequest.setMsg_name("device_control");
        controlDeviceRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        controlDeviceRequest.setToken(token);
        controlDeviceRequest.setDevice(device);
        NetManager.getInstance().sendMessage(JsonUtil.toString(controlDeviceRequest));
    }

    @Override
    public void irLearn(@NonNull String userName, @NonNull String token, @NonNull String sn,
                        @NonNull IrLearnRequest.Device device, @NonNull CallBack<LoginResponse> callBack) {
        IrLearnRequest irlearnRequest = new IrLearnRequest();
        irlearnRequest.setFrom(userName);
        irlearnRequest.setTo(sn);
        irlearnRequest.setMsg_id(callBack.getMsgId());
        irlearnRequest.setMsg_class(ConstantGateway.MSG_CLASS_CONTROL);
        irlearnRequest.setMsg_name("ir_learn");
        irlearnRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        irlearnRequest.setToken(token);
        irlearnRequest.setDevice(device);
        NetManager.getInstance().sendMessage(JsonUtil.toString(irlearnRequest));
    }


}
